package com.system.manager.modules.risk.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.system.manager.modules.authorized.entity.User;
import com.system.manager.modules.risk.dto.FeedBackVo;
import com.system.manager.modules.risk.entity.Risk;
import com.system.manager.modules.risk.entity.TbFeedback;
import com.system.manager.modules.risk.entity.TbFlow;
import com.system.manager.modules.risk.mapper.RiskMapper;
import com.system.manager.modules.risk.mapper.TbFeedbackMapper;
import com.system.manager.modules.risk.mapper.TbFlowMapper;
import com.system.manager.modules.risk.service.ITbFeedbackService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.system.manager.modules.risk.vo.UserDate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author yxp
 * @since 2021-11-16
 */
@Service
public class TbFeedbackServiceImpl extends ServiceImpl<TbFeedbackMapper, TbFeedback> implements ITbFeedbackService {

    @Autowired
    private TbFlowMapper tbFlowMapper;

    @Autowired
    private TbFeedbackMapper tbFeedbackMapper;

    @Autowired
    private RiskMapper riskMapper;
    //查看反馈
    @Override
    public UserDate<TbFeedback> findFeedBack(User user) {
        Integer role = user.getRole();
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("fb_issue_role",role);
        List feedbackList = tbFeedbackMapper.selectList(wrapper);
        UserDate<TbFeedback> userDate = new UserDate<>();
        userDate.setCode(0);
        userDate.setCount(feedbackList.size());
        userDate.setMsg("意见反馈");
        userDate.setData(feedbackList);
        return userDate;
    }
    //上传反馈到反馈表
    @Override
    public void createFeedback(User user, FeedBackVo feedBackVo) {
        TbFeedback tbFeedback = new TbFeedback();
        //查看该意见属于哪个业务
        Integer fbType = feedBackVo.getFbType();
        tbFeedback.setFbType(fbType);
        //意见内容
        String fbMessage = feedBackVo.getFbMessage();
        tbFeedback.setFbMessage(fbMessage);
        //反馈者
        Integer userId = user.getUserId();
        tbFeedback.setFbUser(userId);
        //反馈时间
        Date date = new Date();
        tbFeedback.setFbDate(date);
        //反馈处理人
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("f_id",Integer.valueOf(fbType));
        TbFlow tbFlow = tbFlowMapper.selectOne(wrapper);
        Integer roleUser = tbFlow.getRoleUser();
        Integer role = tbFlow.getRole();
        tbFeedback.setFbIssue(roleUser);
        //反馈处理人的角色
        tbFeedback.setFbIssueRole(Integer.valueOf(role));
        tbFeedback.setFb_state(0);
        tbFeedbackMapper.insert(tbFeedback);

        //还要上传到风险表
        Risk risk = new Risk();
        risk.setRName("意见反馈");
        //风险发布人
        String name = user.getName();
        risk.setRIssue(name);
        //风险发布人的手机
        String phone = user.getPhone();
        risk.setRPhone(phone);
        //风险分类
        risk.setRKey(2);
        //创建时间
        risk.setCreateTime(date);
        //对应业务
        risk.setFId(Integer.valueOf(fbType));
        riskMapper.insert(risk);
    }


}
